package com.lc.pianostudent.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lc.pianostudent.BaseApplication;
import com.lc.pianostudent.R;
import com.lc.pianostudent.activity.ContactDetailActivity;
import com.lc.pianostudent.adapter.ContactAdapter;
import com.lc.pianostudent.adapter.HomeNewsAdapter;
import com.lc.pianostudent.bean.ContactBean;
import com.lc.pianostudent.bean.HomeNewsBean;
import com.lc.pianostudent.conn.PostGetmail;
import com.lc.pianostudent.view.ScrollLinearLayoutManager;
import com.lc.pianostudent.view.SideBar;
import com.zcx.helper.app.AppCallBack;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/4/18.
 */

public class ContactFragment extends BaseFragment{
    private List<ContactBean> contactList = new ArrayList<>();

    @BoundView(R.id.contact_recycle_view) private RecyclerView recyclerView;
    private ContactAdapter contactAdapter;

    @BoundView(R.id.side_letter_bar)
    private SideBar mLetterBar;
    private LinearLayoutManager mLayoutManager;
    private boolean move;

    //联系人
    private PostGetmail postGetmail = new PostGetmail(new AsyCallBack<PostGetmail.PostGetmailInfo>() {



        @Override
        public void onSuccess(String toast, int type, Object o, PostGetmail.PostGetmailInfo postGetmailInfo) throws Exception {

            if(type == 0){
                contactList.clear();
            }
            contactList.addAll(postGetmailInfo.contactList);
            contactAdapter.notifyDataSetChanged();
        }

        @Override
        public void onEnd(String toast, int type) throws Exception {
//            recyclerView.refreshComplete();
//            recyclerView.loadMoreComplete();
        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }

    });
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = BoundViewHelper.boundView(this, inflater.inflate(R.layout.fragment_contact, null));

        initView();
        return  view;
    }

    private void initView(){
        setAppCallBack(new CallBack());


        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        contactAdapter = new ContactAdapter(getActivity(), contactList);
        recyclerView.setAdapter(contactAdapter);

        postGetmail.id = BaseApplication.BasePreferences.readUID();
        postGetmail.type = "0";
        postGetmail.execute(getContext(),false,0);


        mLetterBar.setOnLetterChangedListener(new SideBar.OnLetterChangedListener() {
            @Override
            public void onLetterChanged(String letter) {
                Log.e("letter---",letter+"");
                int position = 0;
                for(int i = 1;i<contactList.size();i++){
                    ContactBean contactBean = contactList.get(i);
                    String downletter = contactBean.getLetter();
                    Log.e("---downletter",downletter+"");
                    if(downletter.equals(letter)){
                        position = i;
                    }
                }

                mLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                mLayoutManager.scrollToPositionWithOffset(position, 0);
            }
        });


    }

    public class CallBack implements AppCallBack {

        public void onRefresh() {


            postGetmail.id = BaseApplication.BasePreferences.readUID();
            postGetmail.type = "0";
            postGetmail.execute(getContext(),false,0);

        }

    }
}
